/**
 * Created by zhaobowen on 2017/3/13.
 */
module.controller('configAddController',
    function ($scope,$http,$state,$rootScope,$cookies,$q,$window,$stateParams,storageService,k8sService,VolMgrConf,netTesting,svcCommonService,$mdDialog,AppConfigService,utilService,params) {

        var user = $cookies.get("username");
        $scope.sub= {};
        $scope.envInf= {};
        $scope.curEnv = {};
        $scope.focus=false;

        function _initCurEnvInfo(envInfo){
            envInfo.name = undefined;
            envInfo.value = undefined;
        }
        function isEmpty(value) {
            return !value || value == "";
        }
        function loadEnvs(){
            AppConfigService.getlistEnv(user,params.imageName,params.version).then(function(ret){
                $scope.sub.envInfos = ret.slice();
                $scope.sub.envInfos.forEach(function(env){
                    env.info = [];
                    for(var key in env.value){
                        env.info.push({name:key,value:env.value[key]})
                    }
                })
                // console.log($scope.sub.envInfos);
            },function(err){
                alertify.error("load ["+params.imageName+":"+params.version+"] env error:"+err.data);
            })
        }
        loadEnvs();

        $scope.submit = function(data){
            // console.log($scope.sub.envInfos);
            var dbEnv = {};
            $scope.sub.envInfos.forEach(function(envSet){
                if(envSet.info !== undefined){
                    dbEnv[envSet.name] = {};
                    envSet.info.forEach(function(env){
                        dbEnv[envSet.name][env.name] = env.value;
                    })
                }
            });
            params.envs = dbEnv;
            AppConfigService.updateConfigEnv(params).then(function(ret){
                alertify.success('update env set success');
                $mdDialog.cancel();
            },function(err){
                alertify.error('update env set error:'+err.data);
                $mdDialog.cancel();
            });
            $http.post('appConfig/addEnv',data).then(function (response) {
                // console.log(response);
            });
        };

        $scope.addClassActive = function(index){

          var Top=jQuery("#configCreateGroup dd.linePosition").eq(index).position().top;
          jQuery(".configLine").css('top',parseInt(Top)-2+"px");
            $scope.activeSelected=index;
        };

        $scope.newEnv=function () {
            if($scope.curEnv !== undefined  && $scope.curEnv.name !== "" && $scope.curEnv.name !== undefined){
                var _item = utilService.deepCopyObj($scope.curEnv);
                $scope.sub.envInfos.push(_item);
                $scope.curEnv = {};
            }
        }

        $scope.addEnv = function(envInfo,index) {
            if (isEmpty(envInfo.name) || isEmpty(envInfo.value)) {
                alertify.alert("环境变量的名称和值都不能为空");
                return;
            }
            var _item = utilService.deepCopyObj(envInfo);
            $scope.sub.envInfos[index].info = $scope.sub.envInfos[index].info || [];
            $scope.sub.envInfos[index].info.push(_item);
            _initCurEnvInfo(envInfo);
        };

        $scope.removeSet=function(index){
            $scope.sub.envInfos.splice(index, 1);
        }
        $scope.deleteEnv = function(index,i) {
            $scope.sub.envInfos[$scope.activeSelected].info.splice(index,1);
        }

        $scope.cancel=function () {
            $mdDialog.cancel();
        }
        $scope.modalCheckedFun=function () {
          $scope.modalChecked=!$scope.modalChecked
        };


    }
);